<script setup>
import SvgIcon from "@/components/SvgIcon/index.vue";
import { useUserNavigation } from "@/hooks/useUserNavigation"; // 导入我们的 Hook
import { onMounted } from "vue";

const { navigateToUserProfile } = useUserNavigation();

const props = defineProps({
  author: {
    type: Object,
    default: () => ({
      username: "",
      avatar: "",
    }),
  },
  description: {
    type: String,
    default: "",
  },
  likeCount: {
    type: Number,
    default: 0,
  },
  commentCount: {
    type: Number,
    default: 0,
  },
  shareCount: {
    type: Number,
    default: 0,
  },
  isLike: {
    type: Boolean,
    default: false,
  },
  favoritesCount: {
    type: Number,
    default: 0,
  },
  isFavorited: {
    type: Boolean,
    default: false,
  },
  isFollowed: {
    type: Boolean,
    default: false,
  }
});

const emit = defineEmits(["like", "comment", "favorites", "follow"]);

const handleLike = () => {
  emit("like");
};

const handleComment = () => {
  emit("comment");
};

const handleFavorites = () => {
  emit("favorites");
};

const handleFollow = () => {
  emit("follow");
};

// 处理用户名点击
const handleUsernameClick = (event) => {
  // 阻止事件冒泡到视频播放器
  event.stopPropagation();
  
  if (props.author && props.author.id) {
    navigateToUserProfile(props.author.id);
  }
};

onMounted(() => {
  // console.log("VideoInfo mounted");
});
</script>

<template>
  <div class="video-info">
    <div class="video-actions">
      <div class="action-item" @click.stop="handleLike">
        <!-- 基本使用 -->
        <SvgIcon :icon-class="isLike ? 'love' : 'unlove'" :size="32" />
        <span class="text-class">{{ likeCount }}</span>
      </div>
      <div class="action-item" @click.stop="handleFavorites">
        <SvgIcon
          :icon-class="isFavorited ? 'collection' : 'uncollection'"
          :size="32"
        />
        <span class="text-class">{{ favoritesCount }}</span>
      </div>
      <div class="action-item" @click.stop="handleComment">
        <SvgIcon icon-class="comments" :size="32" />
        <span class="text-class">{{ commentCount }}</span>
      </div>
    </div>

    <div class="author-info">
      <div class="avatar-container">
        <img :src="author.avatar" alt="avatar" class="avatar" />
        <div
          class="follow-btn"
          @click.stop="handleFollow"
          :class="{ followed: isFollowed }"
        >
          {{ isFollowed ? "✓" : "+" }}
        </div>
      </div>
    </div>

    <div class="video-description">
      <div class="author-name" @click.stop="handleUsernameClick">@{{ author.username }}</div>
      <div class="description-text">{{ description }}</div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.video-info {
  padding: 16px;
  color: white;
  width: 100%;
  pointer-events: auto;

  .video-actions {
    position: absolute;
    right: 16px;
    bottom: 120px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;

    .action-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      cursor: pointer;

      i {
        font-size: 32px;
        margin-bottom: 4px;
      }

      span {
        font-size: 14px;
      }

      .active {
        animation: pop 0.3s ease;
      }
    }
  }

  .author-info {
    margin-bottom: 12px;

    .avatar-container {
      display: flex;
      align-items: center;

      .avatar {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        border: 2px solid white;
        object-fit: cover;
      }

      .follow-btn {
        width: 20px;
        height: 20px;
        background-color: #3490dc;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        left: -10px;
        cursor: pointer;
        font-size: 16px;
        font-weight: bold;
        transition: all 0.3s ease;

        &.followed {
          background-color: #4caf50; // 已关注时显示绿色
        }
      }
    }
  }

  .video-description {
    padding: 6px;
    .author-name {
      font-weight: bold;
      margin-bottom: 4px;
      cursor: pointer; /* 添加鼠标指针样式 */
      display: inline-block; /* 使点击区域仅限于文本 */
    }

    .description-text {
      font-size: 14px;
      line-height: 1.4;
    }
  }
}

.text-class {
  margin-top: 10px;
}

@keyframes pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}
</style>
